package com.mayspeed.jg.gui;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.mayspeed.jg.JingGong;
import com.mayspeed.jg.util.JGUtil;

/**
 * Gui类
 * @author May_Speed
 * @since 2017/4/22
 */
public class JGGui {
	
	/**
	 * 关于按钮
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ItemStack getAboutItem() {
		ItemStack is = new ItemStack(Material.getMaterial(JingGong.getInstance().getConfig().getInt("AboutItem.Material")) ,1, (short)JingGong.getInstance().getConfig().getInt("AboutItem.Data"));
		ItemMeta im = is.getItemMeta();
		im.setDisplayName(JingGong.getInstance().getConfig().getString("AboutItem.DisplayName").replaceAll("&", "§"));
		List <String> lore = new ArrayList<String>();
		for (String string : (JingGong.getInstance().getConfig().getStringList("AboutItem.Lore"))) {
			lore.add(string.replaceAll("&", "§"));
		}
		im.setLore(lore);
		is.setItemMeta(im);
		return is;
	}
	
	/**
	 * 确认按钮
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ItemStack getYestem() {
		ItemStack is = new ItemStack(Material.getMaterial(JingGong.getInstance().getConfig().getInt("YesItem.Material")) ,1, (short)JingGong.getInstance().getConfig().getInt("YesItem.Data"));
		ItemMeta im = is.getItemMeta();
		im.setDisplayName(JingGong.getInstance().getConfig().getString("YesItem.DisplayName").replaceAll("&", "§"));
		List <String> lore = new ArrayList<String>();
		for (String string : (JingGong.getInstance().getConfig().getStringList("YesItem.Lore"))) {
			lore.add(string.replaceAll("&", "§"));
		}
		im.setLore(lore);
		is.setItemMeta(im);
		return is;
	}
	
	/**
	 * 价钱按钮
	 * @param player
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static ItemStack getCostItem(Player player) {
		ItemStack is = new ItemStack(Material.getMaterial(JingGong.getInstance().getConfig().getInt("CostItem.Material")) ,1, (short)JingGong.getInstance().getConfig().getInt("CostItem.Data"));
		ItemMeta im = is.getItemMeta();
		im.setDisplayName(JingGong.getInstance().getConfig().getString("CostItem.DisplayName").replaceAll("&", "§").replaceAll("%cost%", String.valueOf(JingGong.getInstance().getConfig().getInt("Costs." + (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)) + 1)))));
		List <String> lore = new ArrayList<String>();
		
		for (String string : (JingGong.getInstance().getConfig().getStringList("CostItem.Lore"))) {
			lore.add(string.replaceAll("&", "§").replaceAll("%cost%", String.valueOf(JingGong.getInstance().getConfig().getInt("Costs." + (JGUtil.getItemJingGongLevel(JGUtil.getItemInMainHand(player)) + 1)))));
		}
		im.setLore(lore);
		is.setItemMeta(im);
		return is;
	}
	
	/**
	 * 打开Gui
	 * @param player
	 */
	public static void openGui(Player player) {
	    Inventory Gui = Bukkit.createInventory(null, 9, JingGong.getInstance().getConfig().getString("Gui.Title").replaceAll("&", "§"));
	    Gui.addItem(JGUtil.getItemInMainHand(player));
	    Gui.addItem(getAboutItem());
	    Gui.setItem(4 ,getYestem());
	    Gui.setItem(8 ,getCostItem(player));
	    player.closeInventory();
	    player.openInventory(Gui);
	  }
}
